



;;;; todo:
;;;; add preview to this pane, so you can see
;;;; some of the materials that are in each item.
;;;; as you click each item.
;;;; (basically, run the query each time an item is selected instead of hitting button)
;;;; not sure its necessary here.


(defun returnindex ()
  ;;This function loads the template into the main window from the listbox.
  ;;Here, we use sql to do the alphabetical sort via ORDER BY,
  ;;so we don't have to worry
  ;;about it in the lists/lisp. Note that this isn't representative of
  ;;the table's normal index.
  (setf chosen-item (listbox-get-selection countries)) ;;returns number(int) in list. e.g. Make list to test (setf myvalue (cons 4 nil))
  ;; User didn't select anything:
  (if (not chosen-item) ;;NIL is equivalent to boolean false. [ if (not value == if (!value) ]
      (dotimes (x 1)
	(error-report "Please select a formula." 523421) ;;this can't be here, or return-from won't work... must be breaking out of this loop with do-msg
	(return-from returnindex))  ;;result: nothing happens. exit this function. todo: warning message.
      )
  ;;get Primary Key: timedateunix. Will need this to use a foreign key for Templates_materials. 
  (setf dbreturn  (execute-single *db* "SELECT Timedateunix FROM Templates_base ORDER BY FORMULA LIMIT 1 OFFSET ?;" (car chosen-item)))
  ;;start populating data
  (setf *templatelistreturn* (car (execute-to-list *db* "select Formula, Quantity, Customer, Notes from Templates_base where Timedateunix = ?" dbreturn))) ;;car as it returns a list in a list.
  ;;parse / enter templates_base into entries
  (setf (text *top_e_formula* ) (nth 0 *templatelistreturn*)) ;;ltk library specific text assignment (text widget)
  (setf (text *top_e_quantity*) (nth 1 *templatelistreturn*))
  (setf (text *top_e_customer*) (nth 2 *templatelistreturn*))
  (setf (text *top_e_date*)     (get-readable-time dbreturn))
  (setf (text *top_e_notes*)    (nth 3 *templatelistreturn*))
  (setf (text *top_e_lot*) "") ;;blank out
  
  ;;parse / enter templates_materials into entries
  (setf *templatelistreturn* (car (execute-to-list *db* "select Lbs, Oz, Grams, Materials, Internal from Templates_materials where Timedateunix = ?" dbreturn))) 
  ;;process for multiple rows / lists in list
  ;;get count of rows
  (setf  *sqlite_row_count* (execute-single *db* "select COUNT(Materials) from Templates_materials where Timedateunix = ? ;" dbreturn))
  ;;go through #'s equal to count and populate based on count
  (loop for x from 1 to 6 do
    (setf *templatelistreturn* (car (execute-to-list *db*
						     "select Lbs, Oz, Grams, Materials, Internal from Templates_materials where Timedateunix = ? order by Lbs ASC, Oz ASC, Grams ASC LIMIT 1 OFFSET ?;"
						     dbreturn (- x 1)))) ;;sqlite row starts at 0
    (setf *MAT_LOOP_MAT*
	  (symbol-in-symbol *MAT_LOOP_MAT* "*ENTRY_MAT" (write-to-string x) "*")) ;;this concatenate like function will create the entry name
    (setf *MAT_LOOP_LBS*
	  (symbol-in-symbol *MAT_LOOP_LBS* "*ENTRY_LBS" (write-to-string x) "*"))
    (setf *MAT_LOOP_OZ*
	  (symbol-in-symbol *MAT_LOOP_OZ*  "*ENTRY_OZ"  (write-to-string x) "*")) 
    (setf *MAT_LOOP_GMS*
	  (symbol-in-symbol *MAT_LOOP_GMS* "*ENTRY_GMS" (write-to-string x) "*"))
    (setf *MAT_LOOP_INT*
	  (symbol-in-symbol *MAT_LOOP_INT* "*ENTRY_INT" (write-to-string x) "*"))
    (setf (text (get-symbol-in-symbol *MAT_LOOP_LBS*)) (nth 0 *templatelistreturn*))
    (setf (text (get-symbol-in-symbol *MAT_LOOP_OZ* )) (nth 1 *templatelistreturn*))
    (setf (text (get-symbol-in-symbol *MAT_LOOP_GMS*)) (nth 2 *templatelistreturn*))
    (setf (text (get-symbol-in-symbol *MAT_LOOP_MAT*)) (nth 3 *templatelistreturn*))
    (setf (text (get-symbol-in-symbol *MAT_LOOP_INT*)) (nth 4 *templatelistreturn*))
    
	)
  ;;(return-from returnindex)  
  )

;;delete template. some redundant code.
(defun deletetemplate ()
  (defparameter yuno_delete_template NIL)
  (setf chosen-item (listbox-get-selection countries))
  (if (not chosen-item) 
      (dotimes (x 1)
	(error-report "Please select a formula." 523421) 
	(return-from returnindex))
      )
  (setf dbreturn  (execute-single *db* "SELECT Timedateunix FROM Templates_base ORDER BY FORMULA LIMIT 1 OFFSET ?;" (car chosen-item)))
  ;;delete
  (setf yuno_delete_template (format nil "~a" (ask-yesno "Are you sure you want to delete?"))) ;;returns string
  (if (string-equal yuno_delete_template "T")
      ;;if true delete
      (sqlite:execute-non-query *db* "delete from templates_base where Timedateunix = ?" dbreturn)
      ;;else, exit function
      (return-from deletetemplate)
      )
  ;;clear & repopulate listbox
  (listbox-clear (listbox countries))
  (setf *country-names* (execute-to-list *db* "select Formula from Templates_base Order By Formula")) ;;must re-run sql
  (listbox-append countries *country-names*)
  )


  ;;This is the window where the Load template button on the main page
  ;;will direct to. Allows user to load a template from the database.
(defun loadtemplatedialog ()
  (setf *templateloadwin* (make-instance 'toplevel :width 500 :height 400
						   :padx 5 :pady 5
						   :title "Load Template"))
  (setf content (make-instance 'frame :master *templateloadwin*
				      :width 500 :height 400))
  (setf *country-names* (execute-to-list *db* "select Formula from Templates_base Order By Formula"))

  
  (setf countries (make-instance 'scrolled-listbox :master content ))

  
  (listbox-append countries *country-names*)
  (configure (listbox countries) :height 40 :width 35)

  (grid content 0 0)
  (grid countries 0 0  :sticky "nsew")
  ;;(pack countries :fill :both :expand t)
  ;; the following only valid for pack: :fill :both :expand t
  
  ;;todo: status text with full name at bottom, in case of cutoff....
  ;;(bind (listbox countries) "<<ListboxSelect>>"
  ;;	#'(lambda (evt) (returnindex)))

  ;;todo: log selection to text file
  
  ;;select and cancel buttons
  (setf *select_template* (make-instance 'button :text "Select Template"
						 :command #'(lambda () (returnindex))
						 :master content))

  (grid *select_template*  0 1  :sticky "ne" :pady 5 :padx 5)
  (grid-columnconfigure content 0 :weight 1)
  (grid-columnconfigure content 1 :weight 1)
  (grid-rowconfigure content 0 :weight 1)
  (grid-rowconfigure content 1 :weight 1)
  (grid-rowconfigure content 2 :weight 1)

  
  (setf *cancel_template* (make-instance 'button
					 :text "Exit"
					 :command #'(lambda () (destroy *templateloadwin*))
					 :master content))
  (grid *cancel_template*  1 1  :sticky "ne" :pady 5 :padx 5)

  (setf *delete_template* (make-instance 'button :text "Delete Template"
						 :command #'(lambda () (deletetemplate))
						 :master content))

  (grid *delete_template*  2 0  :sticky "nw" :pady 5 :padx 5)
  )



